package com.sh.controller;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.sh.common.ResponseInfoEnum;
import com.sh.entity.MonthBalanceRPT;
import com.sh.entity.ReceivePayRPT;
import com.sh.entity.User;
import com.sh.service.RptService;
import com.sh.util.DateUtil;

/**
 * 报表 相关 controller
 * */
@RestController
@RequestMapping("/api/rpt")
public class RptController extends BaseController{
	@Autowired
	RptService rptService;

	/**
	 * 查询 应收报表
	 *
	 * */
	@ResponseBody
	@RequestMapping("/listReceiveRPT")
	public Object listReceiveRPT(
			@RequestParam(value = "startDate",required = false) String startDate,
			@RequestParam(value = "endDate",required = false) String endDate,
			@RequestParam(value = "customerName",required = false) String customerName){

		List<ReceivePayRPT> list = null;
		try {
			User user = null;
			String sysTime = DateUtil.getSysTime("yyyy-MM-dd hh:mm:ss");

			Map<String,Object> conMap = new HashMap<String,Object>();
			conMap.put("startDate",startDate);
			conMap.put("endDate", endDate);
			conMap.put("customerName", customerName);

			if(StringUtils.isEmpty(startDate)||
					StringUtils.isEmpty(endDate)){
				return failForService("400", ResponseInfoEnum.RECEIVE_RPT_QUERY_FAILED,"开始日期 或者 结束日期 必须填写!");
			}



			list = rptService.listReceiveRPT(conMap,user,sysTime);

		} catch (ParseException e) {
			// TODO Auto-generated catch block
			log.error(e.getMessage());
			return failForService("400", ResponseInfoEnum.RECEIVE_RPT_QUERY_FAILED,e.getMessage());

		}
		return succForService(ResponseInfoEnum.RECEIVE_RPT_QUERY_SUCCESS, list);

	}

	/**
	 * 查询 应付报表
	 *
	 * */
	@ResponseBody
	@RequestMapping("/listPayRPT")
	public Object listPayRPT(
			@RequestParam(value = "startDate",required = false) String startDate,
			@RequestParam(value = "endDate",required = false) String endDate,
			@RequestParam(value = "customerName",required = false) String customerName){

		List<ReceivePayRPT> list = null;
		try {
			User user = null;
			String sysTime = DateUtil.getSysTime("yyyy-MM-dd hh:mm:ss");

			Map<String,Object> conMap = new HashMap<String,Object>();
			conMap.put("startDate",startDate);
			conMap.put("endDate", endDate);
			conMap.put("customerName", customerName);

			if(StringUtils.isEmpty(startDate)||
					StringUtils.isEmpty(endDate)){
				return failForService("400", ResponseInfoEnum.PAY_RPT_QUERY_FAILED,"开始日期 或者 结束日期 必须填写!");
			}


			list = rptService.listPayRPT(conMap,user,sysTime);

		} catch (ParseException e) {
			// TODO Auto-generated catch block
			log.error(e.getMessage());
			return failForService("400", ResponseInfoEnum.PAY_RPT_QUERY_FAILED,e.getMessage());

		}
		return succForService(ResponseInfoEnum.PAY_RPT_QUERY_SUCCESS, list);

	}

	/**
	 * 查询 月度余额报表
	 *
	 * */
	@ResponseBody
	@RequestMapping("/listMonthlyBalanceRPT")
	public Object listMonthlyBalanceRPT(
			@RequestParam(value = "startMonth",required = false) String startMonth,
			@RequestParam(value = "endMonth",required = false) String endMonth,
			@RequestParam(value = "customerName",required = false) String customerName){

		List<MonthBalanceRPT> list = null;
		try {
			User user = null;
			String sysTime = DateUtil.getSysTime("yyyy-MM-dd hh:mm:ss");

			Map<String,Object> conMap = new HashMap<String,Object>();
			conMap.put("startMonth",startMonth);
			conMap.put("endMonth", endMonth);
			conMap.put("customerName", customerName);

			list = rptService.listMonthlyBalanceRPT(conMap,user,sysTime);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error(e.getMessage());
			return failForService("400", ResponseInfoEnum.MONTHLYBALANCEY_RPT_QUERY_FAILED,e.getMessage());

		}
		return succForService(ResponseInfoEnum.MONTHLYBALANCE_RPT_QUERY_SUCCESS, list);

	}

	/**
	 * 计算  上个月 的月份余额
	 * */
	@ResponseBody
	@RequestMapping("/calculateBalance")
	public Object calculateBalance(){

		List<MonthBalanceRPT> list = null;
		try {
			User user = getLoginUser();
			String sysTime = DateUtil.getSysTime("yyyy-MM-dd hh:mm:ss");


			//计算 之前判断  上个月对应的年份是不是已经 结转
			String preMonth =
					DateUtil.getPreMonth(DateUtil.getSysTime("yyyy-MM-dd"));
			String year = preMonth.split("-")[0];
			List<String> yearList = new ArrayList<>();
			yearList.add(year);
			String checkDataCarryMsg =
					checkDataCarry(yearList);
			if(!"".equals(checkDataCarryMsg)){
				return failForService("400",
						ResponseInfoEnum.MONTHLYBALANCEY_RPT_CALCULATE_FAILED,checkDataCarryMsg );
			}



			String msg =
					rptService.calculateBalance(user,sysTime);

			if(!"".equals(msg)){
				return failForService("400",
						ResponseInfoEnum.MONTHLYBALANCEY_RPT_CALCULATE_FAILED,msg);
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error(e.getMessage());
			return failForService("400", ResponseInfoEnum.MONTHLYBALANCEY_RPT_CALCULATE_FAILED,e.getMessage());

		}
		return succForService(ResponseInfoEnum.SYSTEM_SUCCESS, list);

	}


	/**
	 * 计算  自定义事件跨度  的所有月份余额
	 * */
	@ResponseBody
	@RequestMapping("/calculateCustomBalance")
	public Object calculateCustomBalance(
			@RequestParam(value = "startMonth",required = false) String startMonth
	){

		List<MonthBalanceRPT> list = null;
		try {
			//默认  结束日期为上个月
			String endMonth = DateUtil.getPreMonth(null);

			if(StringUtils.isEmpty(startMonth)){
				return failForService("400",
						ResponseInfoEnum.MONTHLYBALANCEY_RPT_CALCULATE_FAILED,"开始月份不能为空!");
			}
			if(startMonth.compareTo(endMonth)>0){
				return failForService("400",
						ResponseInfoEnum.MONTHLYBALANCEY_RPT_CALCULATE_FAILED,"开始月份不能 大于 结束月份!");
			}

			//计算 时间跨度中  对应的年份是不是已经 结转
			//拿取时间跨度中的所有年份
			List<String> yearList =
					DateUtil.getBetweenYear(startMonth, endMonth);
			String checkDataCarryMsg =
					checkDataCarry(yearList);
			if(!"".equals(checkDataCarryMsg)){
				return failForService("400",
						ResponseInfoEnum.MONTHLYBALANCEY_RPT_CALCULATE_FAILED,checkDataCarryMsg );
			}





			User user = getLoginUser();
			String sysTime = DateUtil.getSysTime("yyyy-MM-dd hh:mm:ss");

			String msg =
					rptService.calculateCustomBalance(startMonth,endMonth,user,sysTime);

			if(!"".equals(msg)){
				return failForService("400",
						ResponseInfoEnum.MONTHLYBALANCEY_RPT_CALCULATE_FAILED,msg);
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error(e.getMessage());
			return failForService("400", ResponseInfoEnum.MONTHLYBALANCEY_RPT_CALCULATE_FAILED,e.getMessage());

		}
		return succForService(ResponseInfoEnum.SYSTEM_SUCCESS, list);

	}


	/**
	 * 结转
	 * */
	@ResponseBody
	@RequestMapping("/carryForward")
	public Object carryForward(
			@RequestParam(value = "carryYear",required = false) String carryYear
	){

		List<MonthBalanceRPT> list = null;
		try {
			if(StringUtils.isEmpty(carryYear)){
				return failForService("400",
						ResponseInfoEnum.SYSTEM_FAILED,"结转年份不能为空!");
			}
			//如果结转年份是今天 ，一般不能结转
			String curYear = DateUtil.getCurYear(null, "");
			if(carryYear.equals(curYear)){
				return failForService("400",
						ResponseInfoEnum.SYSTEM_FAILED,"还处在当前年份中，不能结转!");
			}

			//已经结转的数据 不能再次结转
			List<String> yearList = new ArrayList<>();
			yearList.add(carryYear);
			String checkDataCarryMsg =
					rptService.checkDataCarry(yearList);
			if(!"".equals(checkDataCarryMsg)){
				return failForService("400",
						ResponseInfoEnum.SYSTEM_FAILED,checkDataCarryMsg);
			}


			User user = getLoginUser();
			String sysTime = DateUtil.getSysTime("yyyy-MM-dd hh:mm:ss");

			String msg =
					rptService.carryForward(carryYear,user,sysTime);

			if(!"".equals(msg)){
				return failForService("400",
						ResponseInfoEnum.SYSTEM_FAILED,msg);
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error(e.getMessage());
			return failForService("400", ResponseInfoEnum.SYSTEM_FAILED,e.getMessage());

		}
		return succForService(ResponseInfoEnum.SYSTEM_SUCCESS, list);

	}

	/**
	 * 判断当前年份是不是已经结转
	 * */
	private String checkDataCarry(List<String> yearList){

		String msg =
				rptService.checkDataCarry(yearList);

		return msg;
	}



}
